package com.ming.springcloud.student.controller;

import com.ming.springcloud.student.pojo.Book;
import com.ming.springcloud.student.pojo.StudentEntity;
import com.ming.springcloud.student.service.IBookService;
import com.ming.springcloud.student.service.IStudentService;
import com.ming.springcloud.user.api.util.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private IBookService bookService;

    /**
     * 查询所有书本信息
     *
     * @return
     */
    @RequestMapping("/findAllBooks")
    public List<Book> findAllStudent() {
        List<Book> BookList = new ArrayList<>();
        List<Book> books = this.bookService.findAll();
        for (Book book :books ) {
            book.setServiceName("我是Service A");
            BookList.add(book);
        }
        return BookList;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @param
     * @return
     */
    @RequestMapping("/findBooksById/{id}")
    public Book  findDataGroupById(@PathVariable("id")Integer id) {
        Book book = new Book();
        Optional<Book> books = this.bookService.findUserById(id);

        book=books.get();
        book.setServiceName("我是Service A");

        return book;
    }

    /**
     * 更新服务器
     *
     * @param
     * @param
     * @return
     */
    //  @RequestMapping(value = "/editStudent" , method = )
    @PostMapping("/editBook")
    public ResponseEntity editDataGroup(@RequestBody Book book) {
        book.setServiceName("我是Service A");
        this.bookService.edit(book);
        return ResponseEntity.success();
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */

    @RequestMapping("/delBook/{id}")
    public ResponseEntity delDataGroup(@PathVariable("id")Integer id) {
        this.bookService.delete(id);
        return ResponseEntity.success();
    }

    /**
     * 添加学生
     * @param book
     * @param
     * @return
     */
    @RequestMapping("/addBook")
    public ResponseEntity addStudent(@RequestBody Book book) {
        book.setServiceName("我是Service A");
        this.bookService.save(book);
        return ResponseEntity.success();
    }
}
